<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="title='美多商城',active='home'">
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="../../css/reset.css">
    <link rel="stylesheet" type="text/css" href="../../css/settle.css">
    <script src="../../js/index.js"></script>
    <script type="text/javascript" src="../../js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="../../js/jquery-ui.min.js"></script>
    <script type="text/javascript" src="../../js/slide.js"></script>
</head>
<body>
<div class="header_con">
    <div class="header">
        <div class="welcome fl">欢迎来到美多商城!</div>
        <div class="fr">
            <div class="login_btn fl" th:if="${session.uaccount} ne null">
                <a href="">
                    欢迎您：<em th:text="${session.uaccount}"></em>
                </a>
                <span>|</span>
                <a href="http://localhost:8896/goods/quit">退出</a>
            </div>
            <div class="login_btn fl" th:if="${session.uaccount} eq null">
                <a href="http://localhost:8893/admin/tologin">
                    请您登陆
                </a>
            </div>
            <div class="user_link fl">
                <span>|</span>
                <a href="/goods/cartAll">我的购物车</a>
                <span>|</span>
                <a href="/goods/getAllorder">我的订单</a>
            </div>
        </div>
    </div>
</div>

<div class="search_bar clearfix">
    <a th:href="@{'getAll?uaccount='+${session.uaccount}}" class="logo fl"><img src="../../img/logo.png"></a>
    <div class="sub_page_name fl">|&nbsp;&nbsp;&nbsp;&nbsp;购物车</div>
    <div class="search_con fr">
        <input type="text" style="background: top" class="input_text fl" name="" placeholder="搜索商品">
        <input type="button" class="input_btn fr" name="" value="搜索">
    </div>
</div>

<div class="box">
    <div class="table-wrapper">
        <form method="get" action="/goods/paygoods" id="payForm">
            <table class="fl-table">
                <thead>
                <tr>
                    <th class="tdone" style="width: 316px">序号</th>
                    <th class="tdtwo">商品名称</th>
                    <th class="tdthree">数量</th>
                    <th class="tdfour">单价</th>
                    <th class="tdfive">小计</th>
                    <th class="tdsix">操作</th>
                </tr>
                </thead>
                <tbody>
                <tr class="trclass" th:each="newgoods:${carts}">
                    <td class="tdone" style="width: 316px">
                        <input  style="border: none;display: none" type="text" name="goodid" th:value="${newgoods.goodid}">
                        <input  style="border: none" type="text" name="id" th:value="${newgoods.id}">
                    </td>
                    <td class="tdtwo " id="name" th:text="${newgoods.goodsname}"></td>
                    <td class="tdthree">
                        <span class="jiajie">
                            <input type="number" value="1" class="num" name="numaa" min="1" max="99" step="1" th:value="${newgoods.number}" oninput="if(value>99)value=99">
                        </span>
                    </td>
                    <td class="tdfour"><span class="unit" th:text="${newgoods.price}"></span></td>
                    <td class="tdfive"><span class="subtal"></span></td>
                    <td class="tdsix">
                        <a th:href="@{'http://localhost:8896/goods/deleteCart?did='+${newgoods.id}}">删&nbsp;除</a>
                    </td>
                </tr>
                <tr class="tr3" style="color: #4e4e4e"><td colspan="6"; class="talast">商品一共 <span style="color:#ff3e3e;font-size: 18px" id="totalItems">2</span> 件
                    <p> 共计花费<span class="pricetal" style="color:#ff3e3e;font-size: 18px" id="totalPrice">8</span> 元</p>
                </td>
                </tr>
                </tbody>
            </table>
            <input type="submit" style="border: none; height:40px; width:100px; background-color:#ff3e3e; font-size:18px ;margin-top: 3%;font-family: 仿宋;color: whitesmoke" value="确定购买">
        </form>
    </div>
</div>

<div class="footer">
    <div class="foot_link">
        <a href="#">关于我们</a>
        <span>|</span>
        <a href="#">联系我们</a>
        <span>|</span>
        <a href="#">招聘人才</a>
        <span>|</span>
        <a href="#">友情链接</a>
    </div>
    <p>CopyRight © 2019 北京美多商城有限公司 All Rights Reserved</p>
    <p>电话：010-****888    京ICP备*******8号</p>
</div>

<!-- 在这里添加JavaScript代码 -->
<script src="../../js/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
    // 计算总数量和总花费
    function calculateTotal() {
        var totalItems = 0;
        var totalPrice = 0;
        $('.trclass').each(function() {
            var num = parseInt($(this).find('.num').val());
            var price = parseFloat($(this).find('.unit').text());
            var subtotal = num * price;
            $(this).find('.subtal').text(subtotal.toFixed(2)); // 更新小计
            totalItems += num;
            totalPrice += subtotal;
        });
        $('#totalItems').text(totalItems);
        $('#totalPrice').text(totalPrice.toFixed(2)); // 保留两位小数
    }

    // 初始化计算总数量和总花费
    $(document).ready(function() {
        calculateTotal();
    });

    // 监听数量变化，重新计算总数量和总花费
    $(document).on('input', '.num', function() {
        calculateTotal();
    });

    // 发送AJAX请求
    $(document).on('submit', '#payForm', function(e) {
        e.preventDefault(); // 阻止表单默认提交行为
        var goodsList = [];
        $('.trclass').each(function() {
            var gid = $(this).find('input[name="goodid"]').val();
            var number = parseInt($(this).find('.num').val());
            var name = $(this).find('td.tdtwo').text();
            var price = parseFloat($(this).find('.unit').text());
            goodsList.push({
                gid: gid,
                number: number,
                name: name,
                price: price
            });
        });
        $.ajax({
            type: 'post', // 注意：这里应该是POST请求
            dataType: 'json',
            url: '/goods/paygoods',
            data: { goodsList: goodsList },
            success: function(response) {
                console.log("购买成功，在我的订单查看购买记录");
                alert("购买成功，在我的订单查看购买记录");
            },
            error: function() {
                alert("购买失败，请稍后再试");
            }
        });
    });
</script>
</body>
</html>